
package com.example.user.coolrun.runnershign;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class OpenGLRenderer implements Renderer {
	private final Group root;
	private long timeAtLastSecond = 0;
	private long currentTimeTaken=0;
	private long starttime = 0;
	private int fpsCounter;
	public int fps = 0;
	
	public boolean firstFrameDone = false;

	public OpenGLRenderer() {
		// Initialize our root.
		Group group = new Group();
		root = group;
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);

		gl.glDisable(GL10.GL_DITHER);

		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		
		//fuer transperancy?
		//gl.glEnable(GL10.GL_ALPHA);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 
        
        timeAtLastSecond = System.currentTimeMillis();
        fpsCounter=0;
	}

	public void onDrawFrame(GL10 gl) {
		if(Settings.RHDEBUG){
			starttime = System.currentTimeMillis();
			currentTimeTaken= System.currentTimeMillis()- starttime;
			Log.d("frametime", "time at beginning: " + Integer.toString((int)currentTimeTaken));
		}
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		// Replace the current matrix with the identity matrix
		gl.glLoadIdentity();
		
		if(Settings.RHDEBUG){
			currentTimeTaken= System.currentTimeMillis()- starttime;
			Log.d("frametime", "time after clear and loadident: " + Integer.toString((int)currentTimeTaken));
		}
		
		// Draw our scene.
		synchronized (root) {
			root.draw(gl);
		}
		fpsCounter++;
		
		
		if(Settings.RHDEBUG){
			currentTimeTaken= System.currentTimeMillis()- starttime;
			Log.d("frametime", "time after draw: " + Integer.toString((int)currentTimeTaken));
		}
		
		if((System.currentTimeMillis() - timeAtLastSecond) > 1000){
			timeAtLastSecond = System.currentTimeMillis();
			fps = fpsCounter;
			fpsCounter=0;
			if(Settings.RHDEBUG) {
				Log.d("framerate", "draws per second: " + Integer.toString(fpsCounter));
			}
		}
		
		firstFrameDone = true;
	}


	public void onSurfaceChanged(GL10 gl, int width, int height) {

		if(Settings.RHDEBUG)
			Log.d("frametime", "onSurfaceChanged called");
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluOrtho2D(gl, 0, width, 0, height);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();


	}
	/*
	  Adds a mesh to the root.
	 */
	public void addMesh(Mesh mesh) {
		synchronized (root) {
			root.add(mesh);	
		}
	}
	
	public void removeMesh(Mesh mesh) {
		synchronized (root) {
			root.remove(mesh);
		}
	}
}
